package com.sosolabs.com.site.client.presenter;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import java.util.logging.Handler;
import com.google.gwt.logging.client.TextLogFormatter;
import java.util.logging.Logger;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.sosolabs.com.site.client.AdUserServiceAsync;
import com.sosolabs.com.site.client.event.AdUserEvent;
import com.sosolabs.com.site.client.event.CellBrowserEvent;
import com.sosolabs.com.site.client.event.CellListEvent;
import com.sosolabs.com.site.client.event.CellSampleEvent;
import com.sosolabs.com.site.client.event.CellTableEvent;
import com.sosolabs.com.site.client.event.CellTreeEvent;
import com.sosolabs.com.site.shared.AdUser;

public class AdUserPresenter implements Presenter {
	public interface Display {
		//HasClickHandlers getConfirmButton();
		HasClickHandlers getTableButton();
		HasClickHandlers getSampleButton();
		HasClickHandlers getListButton();
		HasClickHandlers getTreeButton();
		HasClickHandlers getBrowserButton();
		//HasClickHandlers getButton();
		//HasValue<String> getAccount();
		//HasValue<String> getPassword();
		//void showTable();
		//void save();
		Widget asWidget();
	}
	
	private AdUserServiceAsync rpcService;
	private HandlerManager eventBus;
	private Display display;
	
	private AdUser adUser = new AdUser();
	
	public AdUserPresenter(AdUserServiceAsync rpcService, HandlerManager eventBus, Display display) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		bind();
	}
	
	public void go(final HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}
	
	public void bind(){
		/*display.getConfirmButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CellTableEvent());
				//doSave();
			}
		});*/
		display.getTableButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CellTableEvent());
				//doSave();
			}
		});
		display.getSampleButton().addClickHandler(new ClickHandler() {
	
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CellSampleEvent());
				//doSave();
			}
		});
		display.getListButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CellListEvent());
				//doSave();
			}
		});
		display.getTreeButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CellTreeEvent());
				//doSave();
			}
		});
		
		display.getBrowserButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doSave();
				
				Logger logger = Logger.getLogger("mylogger");
				
				TextLogFormatter text = new TextLogFormatter(true);
				LogRecord rec = new LogRecord(Level.FINEST,"cell browser test begin");
				text.formatMessage(rec);
				
				
				
				logger.log(Level.ALL,"cell browser test begin");
				logger.log(Level.CONFIG,"cell browser test begin");
				logger.log(Level.FINE,"cell browser test begin");
				logger.log(Level.SEVERE,"cell browser test begin");
				logger.log(Level.FINEST,"cell browser test begin");
				logger.log(Level.FINER,"cell browser test begin");				
				logger.log(Level.WARNING,"cell browser test begin");
				logger.log(Level.INFO,"cell browser test begin");
				logger.log(Level.OFF,"cell browser test begin");
				GWT.log("gwt log...");
				eventBus.fireEvent(new CellBrowserEvent());
				//doSave();
			}
		});

		/*display.getButton().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				display.save();
			}
			
		});*/
	}
	
	private void doSave() {
		adUser.setAccount("sdf");
		adUser.setPassword("sdfdsf");
		rpcService.saveAdUser(adUser, new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				Window.alert("result is " + result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
		eventBus.fireEvent(new AdUserEvent());
	}
}
